
//packagesrc.com.easyjava.sample;

importjava.awt.BorderLayout;

importjava.awt.GridBagConstraints;

importjava.awt.GridBagLayout;

import java.awt.Insets;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

import java.util.List;

 

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

importjavax.swing.JMenuItem;

importjavax.swing.JOptionPane;

import javax.swing.JPanel;

importjavax.swing.JScrollPane;

import javax.swing.JTable;

importjavax.swing.JTextField;

importjavax.swing.SwingUtilities;

importjavax.swing.WindowConstants;

importjavax.swing.table.DefaultTableModel;

importjavax.swing.table.TableModel;


/**

 * 

 * 主窗口

 */

public class Main extends javax.swing.JFrame {

private JMenuBar menuBar;

private JButton removeB;

private JTextField nameT;

private JScrollPane panelMiddle;

private JTablecontactTable;

private JButton queryB;

private JLabel nameL;

private JPanel panelTop;

private JMenuItem exitMenu;

private JMenuItem menuNew;

private JMenu opMenu;

//用来访问address表

private AddressDAOaddressDAO = new AddressDAO();

private JButton modifyB;

private JPanel panelBottom;

 

/**

* Auto-generated mainmethod to display this JFrame

*/

public static voidmain(String[] args) {

SwingUtilities.invokeLater(newRunnable() {

public void run() {

Main inst = new Main();

inst.setLocationRelativeTo(null);

inst.setVisible(true);

}

});

}

public Main(){

super();

initGUI();

initTableData();

}

private void initGUI() {

try {

BorderLayout thisLayout =new BorderLayout();

getContentPane().setLayout(thisLayout);

setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

{

panelTop = new JPanel();

getContentPane().add(panelTop,BorderLayout.NORTH);

GridBagLayoutpanelTopLayout = new GridBagLayout();

panelTopLayout.columnWidths= new int[] {67, 7, 7, 7};

panelTopLayout.rowHeights =new int[] {30};

panelTopLayout.columnWeights= new double[] {0.0, 0.1, 0.1, 0.1};

panelTopLayout.rowWeights =new double[] {0.0};

panelTop.setLayout(panelTopLayout);

panelTop.setPreferredSize(newjava.awt.Dimension(501, 44));

{

nameL = new JLabel();

panelTop.add(nameL, newGridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0));

nameL.setText("\u540d\u5b57\uff1a");

}

{

nameT = new JTextField();

panelTop.add(nameT, newGridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL, new Insets(10, 0, 0, 0), 0, 0));

}

{

queryB = new JButton();

panelTop.add(queryB, newGridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,GridBagConstraints.NONE, new Insets(10, 0, 0, 0), 0, 0));

queryB.setText("\u67e5\u8be2");

queryB.addActionListener(newActionListener() {

public voidactionPerformed(ActionEvent evt) {

queryBActionPerformed(evt);

}

});

}

}

{

panelMiddle = newJScrollPane();

getContentPane().add(panelMiddle,BorderLayout.CENTER);

{

contactTable = newJTable();

panelMiddle.setViewportView(contactTable);

}

}

{

panelBottom = new JPanel();

GridBagLayout jPanel1Layout= new GridBagLayout();

jPanel1Layout.columnWidths= new int[] {7, 7, 7, 7};

jPanel1Layout.rowHeights =new int[] {7};

jPanel1Layout.columnWeights= new double[] {0.1, 0.1, 0.1, 0.1};

jPanel1Layout.rowWeights =new double[] {0.1};

getContentPane().add(panelBottom,BorderLayout.SOUTH);

panelBottom.setLayout(jPanel1Layout);

panelBottom.setPreferredSize(newjava.awt.Dimension(501, 28));

{

modifyB = new JButton();

panelBottom.add(modifyB,new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

modifyB.setText("\u4fee\u6539");

modifyB.addActionListener(newActionListener() {

public voidactionPerformed(ActionEvent evt) {

modifyBActionPerformed(evt);

}

});

}

{

removeB = new JButton();

panelBottom.add(removeB,new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

removeB.setText("\u5220\u9664");

removeB.addActionListener(newActionListener() {

public void actionPerformed(ActionEventevt) {

removeBActionPerformed(evt);

}

});

}

}

{

menuBar = new JMenuBar();

setJMenuBar(menuBar);

{

opMenu = new JMenu();

menuBar.add(opMenu);

opMenu.setText("\u64cd\u4f5c");

{

menuNew = new JMenuItem();

opMenu.add(menuNew);

menuNew.setText("\u6dfb\u52a0\u8054\u7cfb\u4eba");

// 添加事件，如果被点击就弹出添加窗口

menuNew.addActionListener(newActionListener() {

public voidactionPerformed(ActionEvent evt) {

showNewDialog();

}

});

}

{

exitMenu = new JMenuItem();

opMenu.add(exitMenu);

exitMenu.setText("\u9000\u51fa");

// 添加事件，如果被点击就退出程序

exitMenu.addActionListener(newActionListener() {

public voidactionPerformed(ActionEvent evt) {

dispose();

}

});

}

}

}

pack();

this.setSize(509, 392);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 弹出添加窗口

*/

private voidshowNewDialog(){

OperationDialog od = newOperationDialog(this);

od.setModal(true);

od.setVisible(true);

initTableData();

}

/**

* 

* 在此方法中处理查询事件

*/

private voidqueryBActionPerformed(ActionEvent evt) {

initTableData();

}

/**

* 更新表格数据

*/

private voidinitTableData(){

List contacts =addressDAO.queryContactsByName(nameT.getText());

//用二维数据存放表格内容

String[][] tableContents =new String[contacts.size()][8];

AddressVO avo;

for (int i=0; i <contacts.size(); i++)

{

avo = (AddressVO)contacts.get(i);

tableContents[i][0] =avo.getAid()+"";

tableContents[i][1] =avo.getName();

tableContents[i][2] =avo.getBirthday();

tableContents[i][3] =avo.getSex() == 1? "男":"女";

tableContents[i][4] =avo.getCellPhone();

tableContents[i][5] =avo.getTelePhone();

tableContents[i][6] =avo.getFavorite();

tableContents[i][7] =avo.getAddress();

}

//把表内容赋给表

TableModelcontactTableModel = 

new DefaultTableModel(

tableContents,

new String[] { "编号", "姓名" ,"生日", "性别","手机", "电话","爱好", "住址"});

contactTable.setModel(contactTableModel);

}

/**

* 

* 打开修改窗口

*/

private voidmodifyBActionPerformed(ActionEvent evt) {

TableModelcontactTableModel = contactTable.getModel();

//取得选中的行

int i =contactTable.getSelectedRow();

if(i<0)

{

JOptionPane.showMessageDialog(this,"请选择表中的一行!");

return;

}

//第一列是aid,获取选定行的aid

String aid =(String)contactTableModel.getValueAt(i, 0);

OperationDialog od = newOperationDialog(this);

od.setModal(true);

od.initData(Integer.parseInt(aid));

od.setVisible(true);

initTableData();

}

/**

* 

* 执行删除

*/

private voidremoveBActionPerformed(ActionEvent evt) {

TableModelcontactTableModel = contactTable.getModel();

//取得选中的行

int i =contactTable.getSelectedRow();

if(i<0)

{

JOptionPane.showMessageDialog(this,"请选择表中的一行!");

return;

}

//第一列是aid,获取选定行的aid

String aid =contactTableModel.getValueAt(i, 0).toString();

addressDAO.delete(Integer.parseInt(aid));

initTableData();

}

}